import { Injectable } from '@nestjs/common';
import { CreateCategoryDto } from './dto/create-category.dto';
import { UpdateCategoryDto } from './dto/update-category.dto';
import { Category } from './entities/category.entity';

@Injectable()
export class CategoryService {
  create(createCategoryDto: CreateCategoryDto) {
    return Category.save({
      ...createCategoryDto,
      isShow: true,
      create_time: new Date(),
      update_time: new Date(),
      delete_time: null,
    });
  }

  async findAll(pageNum: number, pageSize: number) {
    const [list, count] = await Category.findAndCount({
      skip: (pageNum - 1) * pageSize,
      take: pageSize,
    });
    return { list, count };
  }

  findOne(id: number) {
    return Category.findOneBy({ id });
  }

  update(id: number, updateCategoryDto: UpdateCategoryDto) {
    return Category.update(id, { ...updateCategoryDto });
  }

  remove(id: number) {
    return Category.update(id, { delete_time: new Date() });
  }
}
